package com.example.controller;

import com.example.pojo.HeritageProject;
import com.example.service.HeritageProjectService;
import com.example.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/heritageProjects")
public class HeritageProjectController {
    @Autowired
    private HeritageProjectService heritageProjectService;

    //获取所有非遗项目
    @GetMapping("/all")
    public List<HeritageProject> getHeritageProjects() {
        try {
            List<HeritageProject> heritageProjects = heritageProjectService.getAllHeritageProjects();
            return heritageProjects;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //根据非遗分类获取非遗项目
    @GetMapping("/by-category")
    public List<HeritageProject> findAllByCategory(
            @RequestParam(required = false) Integer regionId,
            @RequestParam(required = false) Integer speciesId) {
        return heritageProjectService.findAllByCategory(regionId, speciesId);
    }
    //插入非遗项目
    @PostMapping("/add")
    public void addHeritageProject(@RequestParam("url") MultipartFile url,
                                   @RequestParam("name") String name,
                                   @RequestParam("speciesName") String speciesName,
                                    @RequestParam("regionName") String regionName,
                                    @RequestParam("description") String description,
                                    @RequestParam("content") String content) {
        HeritageProject project = new HeritageProject();
        project.setName(name);
        project.setRegionName(regionName);
        project.setSpeciesName(speciesName);
        project.setDescription(description);
        project.setContent(content);
        String imagePath = null;
        try {
            imagePath = FileUploadUtils.processFileUpload(url);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        project.setUrl(imagePath);
        heritageProjectService.insert(project);
    }
    //更新非遗项目
    @PutMapping("/update/{id}")
    public void updateHeritageProject(@PathVariable Integer id,
                                      @RequestParam(value = "url" ,required = false) MultipartFile url,
                                      @RequestParam(value = "name" ,required = false) String name,
                                      @RequestParam(value = "speciesName",required = false) String speciesName,
                                       @RequestParam(value = "regionName" ,required = false) String regionName,
                                       @RequestParam(value = "description",required = false) String description,
                                       @RequestParam(value = "content",required = false) String content) {
        HeritageProject project = heritageProjectService.findById(id);
        if (name != null) {
            project.setName(name);
        }
        if (regionName != null) {
            project.setRegionName(regionName);
        }
        if (speciesName != null) {
            project.setSpeciesName(speciesName);
        }
        if (description != null) {
            project.setDescription(description);
        }
        if (content != null) {
            project.setContent(content);
        }
        if (url != null && !url.isEmpty()) {
            String imagePath = null;
            try {
                imagePath = FileUploadUtils.processFileUpload(url);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            project.setUrl(imagePath);
        }
        heritageProjectService.update(project);
    }

    //删除非遗项目
    @DeleteMapping("/{id}")
    public void deleteHeritageProject(@PathVariable Integer id) {
        heritageProjectService.delete(id);
    }

    //根据id获取非遗项目
    @GetMapping("/{id}")
    public HeritageProject getHeritageProjectById(@PathVariable Integer id) {
        return heritageProjectService.findById(id);
    }
}
